package exp4.test3;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 春旺 on 2017/11/23.
 */
public class MakeGraph <T>{
    private int EdgeNumber = 0;
    private int vexRemoveRecord = 0;
    private int edgeRemoveRecord = 0;
    private int count = 0;
    ArrayList<T> vexList = new ArrayList<>();
    private int[][] edgeArry;
    public int[][] weight;

    public void addVex(T element) {
        List<Integer> saveList = new ArrayList();
        List<Integer> weightSaveList = new ArrayList<>();
        count++;
        int a = 0;
        for (int i = 0; i < vexList.size(); i++) {
            if (element == vexList.get(i)) {
                a = 1;
                break;
            }
        }
        if (a == 0) {
            int lengthSave = vexList.size();
            if (!vexList.isEmpty()) {
                for (int xIndex = 0; xIndex < vexList.size(); xIndex++) {
                    for (int yIndex = 0; yIndex < vexList.size(); yIndex++) {
                        saveList.add(edgeArry[xIndex][yIndex]);
                        weightSaveList.add(weight[xIndex][yIndex]);
                    }
                }
            }
            vexList.add(element);
            //因为顶点的数量增加了，数组要进行重构，里面的关系的位置要进行变换
            edgeArry = new int[vexList.size()][vexList.size()];
            weight = new int[vexList.size()][vexList.size()];

            for (int xIndex = 0; xIndex < lengthSave; xIndex++) {
                for (int yIndex = 0; yIndex < lengthSave; yIndex++) {
                    edgeArry[xIndex][yIndex] = saveList.remove(0);
                    weight[xIndex][yIndex] = weightSaveList.remove(0);
                    if (yIndex==xIndex){
                        weight[xIndex][yIndex]=0;
                    }
                    if (yIndex == lengthSave - 1) {
                        edgeArry[xIndex][yIndex + 1] = 0;
                        weight[xIndex][yIndex+1] = 10000;
                    }
                }
                if (xIndex == lengthSave - 1) {
                    for (int index = 0; index < vexList.size(); index++) {
                        edgeArry[xIndex + 1][index] = 0;
                        weight[xIndex+1][index] = 10000;
                    }
                }
            }
        } else {
            System.out.println("Vex Add " + count + " : The vertex has been exsisted in the collection.");
        }
    }

    public void addEdged(Edge edge) {
        EdgeNumber++;
        //找第一个点是否存在于点集合中
        int frontVex = 0, endVex = 0;
        int firstIndex = 0, secondIndex = 0;
        for (; firstIndex < vexList.size(); firstIndex++) {
            if (edge.formV == vexList.get(firstIndex)) {
                frontVex = 1;
                break;
            }
        }
        //找不到点1
       if (frontVex == 0) {
//            System.out.println("Edge Add " + EdgeNumber + ": The front vertex is not exsisted.");
        }
        //找得到点1
       else {

            //寻找点2
            for (; secondIndex < vexList.size(); secondIndex++) {
                if (edge.endV == vexList.get(secondIndex)) {
                    endVex = 1;
                    break;
                }
            }

            //找不到点2
            if (endVex == 0) {
               // System.out.println("Edge Add " + EdgeNumber + ": The end vertex is not exsisted.");
            }
            //找得到点2
            else {
                //查看该边是否被占用
                if (edgeArry[firstIndex][secondIndex] == 1) {
                    //System.out.println("Edge Add " + EdgeNumber + ": The edge has been occupied.");
                } else {
                    edgeArry[firstIndex][secondIndex] = 1;
                    edgeArry[secondIndex][firstIndex] = 1;
                    weight[firstIndex][secondIndex]=edge.weight;
                    weight[secondIndex][firstIndex]= edge.weight;
                }

            }
        }

    }



}

